package com.EAIPClub.LiveStreaming.service.impl;

import com.EAIPClub.LiveStreaming.service.LiveStreamingService;
import com.api.API.LiveStreamingManager.LiveStreamingManagerAPI;
import com.api.POJO.LiveStreaming.LiveStreamingBaseInfo;
import com.api.POJO.LiveStreaming.LiveStreamingListInfo;
import com.api.POJO.LiveStreaming.LiveStreamingReturnMsg;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerListInfo;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerListReturnMsg;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/11
 */
@Service
@Slf4j
public class LiveStreamingServiceImpl implements LiveStreamingService {

    private LiveStreamingManagerAPI api;

    @Autowired
    public void setApi(LiveStreamingManagerAPI api) {
        this.api = api;
    }

    @Override
    public LiveStreamingReturnMsg openNewLiveStreaming(String token,LiveStreamingBaseInfo baseInfo) {
        LiveStreamingReturnMsg returnMsg = new LiveStreamingReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将创建新的直播信息.token:{},info:{}",token,baseInfo);
        if (baseInfo==null || token==null || token .equals("")){
            log.warn("检测到信息为空!");
            returnMsg.setStatus(500);
            Msg.add("直播信息为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("即将将直播信息加入到数据库中.info:{}",baseInfo);
        LiveStreamingManagerListReturnMsg liveStreamingManagerListReturnMsg = api.addNewLiveStreamingListInfo(new LiveStreamingManagerListInfo(null, token, baseInfo.getTitle()));
        if (liveStreamingManagerListReturnMsg==null){
            log.warn("将直播信息加入到数据库内失败!返回为空!");
            returnMsg.setStatus(500);
            Msg.add("创建直播间失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (liveStreamingManagerListReturnMsg.getStatus()!=200){
            log.warn("将直播信息加入到数据库内失败!返回:{}",liveStreamingManagerListReturnMsg);
            returnMsg.setStatus(500);
            Msg.addAll(liveStreamingManagerListReturnMsg.getMsg());
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("将直播信息加入到数据库成功!");
        if (baseInfo.getLine().equals("rtmp")){
            log.info("检测到该次直播所选路线为rtmp.token:{},baseinfo:{}",token,baseInfo);
            log.info("即将通过用户token创建rtmp直播流.token:{}",token);
            LiveStreamingManagerReturnMsg liveStreamingManagerReturnMsg = api.addNewRTMPLiveStreamingInfo(token);
            if (liveStreamingManagerReturnMsg==null){
                log.warn("通过token创建用户直播流失败!token:{},返回为空!",token);
                returnMsg.setStatus(500);
                Msg.add("创建rtmp直播流失败!");
                returnMsg.setMsg(Msg);
                return returnMsg;
            }
            if (liveStreamingManagerReturnMsg.getStatus()!=200) {
                log.warn("通过token创建用户直播流失败!token:{},返回:{}", token, liveStreamingManagerReturnMsg);
                returnMsg.setStatus(500);
                Msg.add("创建用户直播流失败!");
                returnMsg.setMsg(Msg);
                return returnMsg;
            }
            log.info("通过用户token创建rtmp直播流成功.token:{},返回:{}",token,liveStreamingManagerReturnMsg);
            returnMsg.setStatus(200);
            returnMsg.setAddress(liveStreamingManagerReturnMsg.getAddress());
            return returnMsg;
        }
        if (baseInfo.getLine().equals("webrtc")){
            log.info("检测到该次直播所选路线为webrtc.token:{},baseinfo:{}",token,baseInfo);
            log.info("即将通过用户token创建webrtc直播流.token:{}",token);
            LiveStreamingManagerReturnMsg liveStreamingManagerReturnMsg = api.addNewWebRtcLiveStreamingInfo(token);
            if (liveStreamingManagerReturnMsg==null){
                log.warn("通过token创建用户直播流失败!token:{},返回为空!",token);
                returnMsg.setStatus(500);
                Msg.add("创建webrtc直播流失败!");
                returnMsg.setMsg(Msg);
                return returnMsg;
            }
            if (liveStreamingManagerReturnMsg.getStatus()!=200) {
                log.warn("通过token创建用户直播流失败!token:{},返回:{}", token, liveStreamingManagerReturnMsg);
                returnMsg.setStatus(500);
                Msg.add("创建用户直播流失败!");
                returnMsg.setMsg(Msg);
                return returnMsg;
            }
            log.info("通过用户token创建webrtc直播流成功.token:{},返回:{}",token,liveStreamingManagerReturnMsg);
            returnMsg.setStatus(200);
            returnMsg.setAddress(liveStreamingManagerReturnMsg.getAddress());
            return returnMsg;
        }
        log.warn("未能正确检测到用户直播所用通道!token:{},baseinfo:{}",token,baseInfo);
        returnMsg.setStatus(500);
        Msg.add("创建直播间失败!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public LiveStreamingReturnMsg getAllLiveStreamingInfo() {
        LiveStreamingReturnMsg returnMsg = new LiveStreamingReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将获取所有直播信息.");
        LiveStreamingManagerListReturnMsg allLiveListInfo = api.getAllLiveListInfo();
        if (allLiveListInfo==null){
            log.warn("获取所有直播基本信息失败!返回为空.");
            returnMsg.setStatus(500);
            Msg.add("获取所有直播信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (allLiveListInfo.getStatus()!=200){
            log.warn("获取所有直播基本信息失败!返回:{}",allLiveListInfo);
            returnMsg.setStatus(500);
            Msg.add("获取所有直播信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        List<LiveStreamingManagerListInfo> datas = allLiveListInfo.getDatas();
        log.info("获取所有信息为:{}",datas);
        log.info("即将通过token获取直播的地址和主播email.");
        List<LiveStreamingListInfo> infoDatas = new ArrayList<>();

        for (LiveStreamingManagerListInfo data : datas) {
            LiveStreamingListInfo temp = new LiveStreamingListInfo();
            temp.setTitle(data.getTitle());
            temp.setImage("http://115.158.28.160/static/img/zxjy.539c321b.jpg");
            String token = data.getToken();
            LiveStreamingManagerReturnMsg rtmpLiveStreamingInfoByToken = api.getRTMPLiveStreamingInfoByToken(token);
            LiveStreamingManagerReturnMsg webRtcLiveStreamingInfoByToken = api.getWebRtcLiveStreamingInfoByToken(token);
            if(rtmpLiveStreamingInfoByToken == null || webRtcLiveStreamingInfoByToken == null){
                log.warn("未能通过token获取到直播地址,返回为空!");
                continue;
            }
            if (rtmpLiveStreamingInfoByToken.getStatus() != 200 && webRtcLiveStreamingInfoByToken.getStatus() != 200) {
                log.warn("未能通过token获取到用户直播信息!data:{},rtmp:{},webrtc:{}",data,rtmpLiveStreamingInfoByToken,webRtcLiveStreamingInfoByToken);
                continue;
            }
            if(rtmpLiveStreamingInfoByToken.getStatus()==200){
                temp.setEmail(rtmpLiveStreamingInfoByToken.getData().getUseremail());
            }
            if(webRtcLiveStreamingInfoByToken.getStatus()==200){
                temp.setEmail(webRtcLiveStreamingInfoByToken.getData().getUseremail());
            }
            LiveStreamingManagerListReturnMsg liveListInfoByToken = api.getLiveListInfoByToken(token);
            if(liveListInfoByToken==null){
                log.warn("未能通过token获取到直播信息!返回为空!");
                continue;
            }
            if(liveListInfoByToken.getStatus()!=200){
                log.warn("未能通过token获取到直播信息!返回:{}",liveListInfoByToken);
                continue;
            }
            temp.setAddress(liveListInfoByToken.getAddress());
            log.info("获取到一个直播信息:{}.",temp);
            infoDatas.add(temp);
        }
        log.info("获取直播信息列表为:{}",infoDatas);
        returnMsg.setStatus(200);
        returnMsg.setDatas(infoDatas);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public LiveStreamingReturnMsg closeLiveStreamingByToken(String token) {
        LiveStreamingReturnMsg returnMsg = new LiveStreamingReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token关闭用户直播.token:{}",token);
        if (token == null || token.equals("")){
            log.warn("检测到token为空!");
            returnMsg.setStatus(500);
            Msg.add("未能获取到token!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        LiveStreamingManagerListReturnMsg liveStreamingManagerListReturnMsg = api.deleteLiveStreamingListInfoByToken(token);
        if(liveStreamingManagerListReturnMsg==null){
            log.warn("未能正确删除直播基本信息!返回为空！");
        }else if(liveStreamingManagerListReturnMsg.getStatus()!=200){
            log.warn("未能正确删除直播基本信息!返回:{}",liveStreamingManagerListReturnMsg);
        }
        LiveStreamingManagerReturnMsg liveStreamingManagerReturnMsg = api.deleteRTMPLiveStreamingInfo(token);
        if(liveStreamingManagerReturnMsg==null){
            log.warn("未能正确删除RTMP直播信息!返回为空！");
        }else if(liveStreamingManagerReturnMsg.getStatus()!=200){
            log.warn("未能正确删除RTMP直播信息!返回:{}",liveStreamingManagerReturnMsg);
        }
        LiveStreamingManagerReturnMsg liveStreamingManagerReturnMsg1 = api.deleteWebRtcLiveStreamingInfo(token);
        if (liveStreamingManagerReturnMsg1==null){
            log.warn("未能正确删除webrtc直播信息!返回为空!");
        }else if(liveStreamingManagerReturnMsg1.getStatus() != 200){
            log.warn("未能正确删除webrtc直播信息!返回:{}",liveStreamingManagerReturnMsg1);
        }
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }
}
